﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data;
using MAT.Model;
using System.Xml;
using System.Xml.Linq;
using System.IO;


namespace MAT.Controllers
{
    class ctrMoveAsset
    {
        public frmMoveAsset _frmMoveAsset = null;

        public frmMoveAsset FrmMoveAsset
        {
            get { return _frmMoveAsset; }
            set { _frmMoveAsset = value; }
        }
        static BindingSource bind = new BindingSource();
        MAT_Context context = new MAT_Context();
        public void btnClose_Click()
        {
            _frmMoveAsset.Close();
            MAT.Controllers.ctrMain._frmMain.pnlMain.Visible = false;
        }
        public void frmMoveAsset_Load()
        {
            //load all room in combobox
            List<ROOM> oldroom = context.ROOMs.ToList();
            _frmMoveAsset.cmbOldRoom.DisplayMember = "room_name";
            _frmMoveAsset.cmbOldRoom.ValueMember = "room_id";
            _frmMoveAsset.cmbOldRoom.DataSource = oldroom;

            List<ROOM> newroom = context.ROOMs.ToList();
            _frmMoveAsset.cmbNewRoom.DisplayMember = "room_name";
            _frmMoveAsset.cmbNewRoom.ValueMember = "room_id";
            _frmMoveAsset.cmbNewRoom.DataSource = newroom;

            //create columns for listview
            _frmMoveAsset.lsvAssetRoom.Columns.Add("ASSET ID", 100);
            _frmMoveAsset.lsvAssetRoom.Columns.Add("ASSET NAME", 200);
            _frmMoveAsset.lsvAssetRoom.Columns.Add("BARCODE", 100);
            _frmMoveAsset.lsvAssetRoom.Columns.Add("BRAND", 100);
            _frmMoveAsset.lsvAssetRoom.Columns.Add("MODULE NUMBER", 100);
            _frmMoveAsset.lsvAssetRoom.Columns.Add("OWNER", 100);
            _frmMoveAsset.lsvOldRoom.Columns.Add("ASSET ID", 100);
            _frmMoveAsset.lsvOldRoom.Columns.Add("ASSET NAME", 150);
            _frmMoveAsset.lsvNewRoom.Columns.Add("ASSET ID", 100);
            _frmMoveAsset.lsvNewRoom.Columns.Add("ASSET NAME", 150);
            _frmMoveAsset.lsvAssetRoom.GridLines = true;
            _frmMoveAsset.lsvOldRoom.CheckBoxes = true;
            _frmMoveAsset.lsvOldRoom.GridLines = true;
            _frmMoveAsset.lsvNewRoom.CheckBoxes = true;
            _frmMoveAsset.lsvNewRoom.GridLines = true;
            cmbOldRoom_SelectedIndexChanged();


        }
        //load asset's information belongs to room at combobox
        public void cmbOldRoom_SelectedIndexChanged()
        {

            _frmMoveAsset.lsvOldRoom.Items.Clear(); //clear listview data before add any one in order to listview get data just one time.
            List<OWNER> owners = context.OWNERS.Where(o => o.ROOM_ID == (string)_frmMoveAsset.cmbOldRoom.SelectedValue).ToList();
            foreach (OWNER owner in owners)
            {
                List<ASSET> assets = context.ASSETs.Where(a => a.EMPLOYEE_ID == owner.EMPLOYEE_ID).ToList();
                foreach (ASSET asset in assets)
                {
                    ListViewItem list = new ListViewItem(asset.ASSET_ID);
                    ListViewItem.ListViewSubItem subItem = new ListViewItem.ListViewSubItem(list, asset.ASSET_NAME);
                    list.SubItems.Add(subItem);
                    _frmMoveAsset.lsvOldRoom.Items.Add(list);
                    //for loop here to against data duplication.
                    for (int i = 0; i < _frmMoveAsset.lsvOldRoom.Items.Count; i++)
                    {
                        for (int j = 1; j < _frmMoveAsset.lsvOldRoom.Items.Count; j++)
                        {
                            if (i != j)
                            {
                                if (_frmMoveAsset.lsvOldRoom.Items[i] == _frmMoveAsset.lsvOldRoom.Items[j])
                                {
                                    _frmMoveAsset.lsvOldRoom.Items[j].Remove();
                                }
                            }
                        }
                    }
                }
            }
        }

        public void btnMove_Click_1()
        {
            try
            {
                // move asset from room to another one. Actually it just change room ID of employee.
                for (int i = 0; i < _frmMoveAsset.lsvNewRoom.Items.Count; i++)
                {
                    string item = _frmMoveAsset.lsvNewRoom.Items[i].Text.Trim();

                    ASSET asset = context.ASSETs.FirstOrDefault(ass => ass.ASSET_ID.Trim() == item);
                    {
                        OWNER owner = context.OWNERS.FirstOrDefault(o => o.EMPLOYEE_ID.Trim() == asset.EMPLOYEE_ID);
                        if (owner == null || _frmMoveAsset.cmbOldRoom.SelectedValue.ToString() == _frmMoveAsset.cmbNewRoom.SelectedValue.ToString())
                        {
                            MessageBox.Show("You could not move asset to the same room!"); break;
                        }
                        else
                        {
                            owner.ROOM_ID = _frmMoveAsset.cmbNewRoom.SelectedValue.ToString();
                            context.SaveChanges();
                            _frmMoveAsset.label4.Text = _frmMoveAsset.cmbNewRoom.Text;
                            xmlWriter(DateTime.Today.ToLongDateString(), _frmMoveAsset.cmbOldRoom.Text, _frmMoveAsset.cmbNewRoom.Text);
                            allAsset();
                            _frmMoveAsset.lsvNewRoom.Items.Clear();
                            MessageBox.Show("Asset is moved successfully!");
                        }
                    } 
                }
            }
            catch (Exception)
            {
                MessageBox.Show("Error!");
            }

        }


        // show all asset of new room in listview.
        public void allAsset()
        {
            List<ROOM> rooms = context.ROOMs.Where(r => r.ROOM_ID == (string)_frmMoveAsset.cmbNewRoom.SelectedValue).ToList();
            foreach (ROOM room in rooms)
            {
                List<OWNER> owners = context.OWNERS.Where(o => o.ROOM_ID == room.ROOM_ID).ToList();
                foreach (OWNER owner in owners)
                {
                    List<ASSET> assets = context.ASSETs.Where(a => a.EMPLOYEE_ID == owner.EMPLOYEE_ID).ToList();
                    foreach (ASSET asset in assets)
                    {
                        OWNER own = context.OWNERS.FirstOrDefault(o => o.EMPLOYEE_ID == asset.EMPLOYEE_ID);
                        ListViewItem list = new ListViewItem(asset.ASSET_ID);
                        ListViewItem.ListViewSubItem sub = new ListViewItem.ListViewSubItem(list, asset.ASSET_NAME);
                        list.SubItems.Add(sub);
                        ListViewItem.ListViewSubItem sub1 = new ListViewItem.ListViewSubItem(list, asset.BARCODE);
                        list.SubItems.Add(sub1);
                        ListViewItem.ListViewSubItem sub2 = new ListViewItem.ListViewSubItem(list, asset.BRAND);
                        list.SubItems.Add(sub2);
                        ListViewItem.ListViewSubItem sub3 = new ListViewItem.ListViewSubItem(list, asset.MODEL_NUM);
                        list.SubItems.Add(sub3);
                        ListViewItem.ListViewSubItem sub4 = new ListViewItem.ListViewSubItem(list, own.EMPLOYEE_NAME);
                        list.SubItems.Add(sub4);
                        _frmMoveAsset.lsvAssetRoom.Items.Add(list);





                    }

                }
            }
        }
        public void button1_Click()
        {

        }
        public void xmlWriter(string Date, string OldRoom, string NewRoom)
        {

            for (int i = 0; i < _frmMoveAsset.lsvNewRoom.Items.Count; i++)
            {

                //initiate nodes with father node (MoveHistory) and child node(AssetName, Date, OldRoom, NewRoom)
                var MAT = new XElement("MoveHistory", new XElement("AssetName", _frmMoveAsset.lsvNewRoom.Items[i].SubItems[i].Text), new XElement("Date", Date), new XElement("OldRoom", OldRoom), new XElement("NewRoom", NewRoom));
                var doc = new XDocument();
                //create xml file.
                if (File.Exists(@"..\..\XML\MoveHistory.xml"))
                {
                    doc = XDocument.Load(@"..\..\XML\MoveHistory.xml");
                    doc.Element("MAT").Add(MAT);//grandfather node (MAT)
                }
                else
                {
                    doc = new XDocument(new XElement("MAT", MAT));
                }
                doc.Save(@"..\..\XML\MoveHistory.xml");
            }
        }

        CommonClass ccl = new CommonClass();
        //move asset from old room to new room.
        public void btnOldtoNew_Click()
        {
            ccl._move(_frmMoveAsset.lsvOldRoom, _frmMoveAsset.lsvNewRoom);
        }
        //move asset from new room return.
        public void btnNewtoOld_Click()
        {
            ccl._move(_frmMoveAsset.lsvNewRoom, _frmMoveAsset.lsvOldRoom);
        }


    }

}
